/**
 * 
 */
package baidu;

import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import com.wixc.entity.Model;
import com.wixc.util.JdbcUtils;
import com.wixc.util.LoginUtil;
import com.wixc.util.MyStringUtils;

/**
 * @author weichuanxi
 * @date 2016年7月7日
 */
public class BaiDuZhiDaoRiBao {
	private static Logger log = Logger.getLogger(BaiDuZhiDaoRiBao.class);
	static ArrayList<Model> all=new ArrayList<Model>();
	public static void main(String[] args) throws FileNotFoundException {
		BaiDu_Run();
	}

	@SuppressWarnings("unchecked")
	public static void BaiDu_Run() {
		LoginUtil util = new LoginUtil();
		String html = util.Get("https://zhidao.baidu.com/daily");
		Document doc = Jsoup.parse(html);
		String total = doc.select("#num-wp").attr("data-num");

		String url = "https://zhidao.baidu.com/daily/ajax/periodList?pn=" + total + "&rn=10";
		html = util.Get(url);
		Map<String, Object> map = MyStringUtils.json2Map(html);
		Map<String, Object> data = (Map<String, Object>) map.get("data");
		List<Map<String, Object>> lists = (List<Map<String, Object>>) data.get("list");
		if (lists.size() > 0) {
			ArrayList<Model> m = deal(util, lists);
			all.addAll(m);
			try {
				JdbcUtils.save(m, "insert into userbean (id,name, password) values (?,?, ?)");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		int t = Integer.parseInt(total);
		for (int i = 0; i < 5; i++) {
			url = "https://zhidao.baidu.com/daily/ajax/periodList?pn=" + (new Random().nextInt(t)) + "&rn=10";
			html = util.Get(url);
			Map<String, Object> map2 = MyStringUtils.json2Map(html);
			Map<String, Object> data2 = (Map<String, Object>) map2.get("data");
			List<Map<String, Object>> lists2 = (List<Map<String, Object>>) data2.get("list");
			if (lists2.size() > 0) {
				ArrayList<Model> m = deal(util, lists2);
				all.addAll(m);
				try {
					JdbcUtils.save(m, "insert into userbean (id,name, password) values (?,?, ?)");
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			i++;
		}
		log.info("success :"+all.size());
	}

	private static ArrayList<Model> deal(LoginUtil util, List<Map<String, Object>> lists) {
		ArrayList<Model> baiduzhidao = new ArrayList<Model>();
		for (Map<String, Object> string : lists) {
			String title = string.get("title") + "";
			String id = string.get("id") + "";
			String html = util.Get("https://zhidao.baidu.com/daily/view?id=" + id);
			Document doc = Jsoup.parse(html);
			String detail = doc.select("daily-cont").html();
			Model m = new Model(0, title, detail);
			baiduzhidao.add(m);
		}
		return baiduzhidao;
	}

}
